<%@ page import="pojo.Course" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 33503
  Date: 2023/11/25
  Time: 0:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>课程信息</title>
    <!--引入jquery和bootstrap -->
    <script type="text/javascript" src="js/jquery-1.12.3.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script>
        function add_stu() {
            var show_part = document.querySelector('.part');
            show_part.style.display = 'block';
        }

        function hide() {
            var show_part = document.querySelector('.part');
            show_part.style.display = 'none';
        }
    </script>
    <style>
        table {
            width: 93.8%;
        }

        th {
            border: 1px solid #000000;
            text-align: center;
            padding: 15px 0;
            background-color: #66afe9;
            height: 40px;
            font-size: 22px;
            color: white;
        }

        td {
            border: 1px solid #000000;
            text-align: center;
            padding: 15px 0;
            height: 40px;
        }

        table tbody tr:hover {
            background-color: #a6e1ec;
        }

        #mytable th {
            text-align: center;
        }

        .content_input .my-input { /*搜索框样式*/
            /*margin-left: 30%;*/
            margin-top: 20px;
            width: 30%;
            height: 38px;
            border-radius: 5px;
            color: black;
            border: 1px solid #3388FF;
        }

        .content_input .search_btn { /*按钮样式*/
            width: 80px;
            height: 40px;
            background-color: dodgerblue;
        }

        .content_input .search_btn:hover { /*按钮覆盖样式*/
            background-color: #2e6da4;
            color: ivory;
        }

        .button-add { /*添加课程按钮样式*/
            width: 90px;
            height: 40px;
            margin-top: 20px;
            margin-right: 30px;
            float: left;
            background-color: #66afe9;
            border-color: #a6e1ec;
            color: white;
            font-size: large;
        }

        .button-add-stu { /*添加学生按钮样式*/
            width: 90px;
            height: 30px;
            text-align: center;
            background-color: #5cb85c;
            border-color: #a6e1ec;
            color: white;
            font-size: medium;
        }

        .button-detail { /*详情按钮样式*/
            width: 90px;
            height: 30px;
            text-align: center;
            background-color: #ffff00;
            border-color: #a6e1ec;
            color: black;
            font-size: medium;
        }

        .getcName {
            border: 1px solid #000000;
            text-align: center;
            padding: 15px 0;
            height: 40px;
        }

        .part { /*用于弹窗样式*/
            display: none;
            background-color: #afd9ee;
            border-radius: 8px;
            font-size: large;
            box-shadow: 4px 4px 4px 4px #337ab7;
            padding-top: 20px;
            padding-left: 30px;
            padding-right: 20px;
            height: 280px;
            width: 340px;
            position: relative;
            margin: 20%;
        }

        .button-inner { /*用于弹窗中的按钮样式*/
            background-color: dodgerblue;
            color: white;
            border-color: #a6e1ec;
            margin-left: 60px
        }
    </style>
</head>
<body>
<%--菜单栏--%>
<jsp:include page="teacher_menu.jsp"></jsp:include>
<div style="background-color: #66afe9;display: inline-block;margin-top: 10px;float: left;margin-left: 20%;height: 80px;width: 200px;text-align: center; border-radius:0px;">
    <h1 style="color:white;text-shadow: 2px 2px 2px dodgerblue;line-height: 40px;border-radius:2px;font-size: 25px">
        课程信息</h1>
</div>
<div style="margin-left: 35%;margin-top:10px;width: 60%;height:80px;background-color: #66afe9; ">
    <form style="padding-left: 10%" action="${pageContext.request.contextPath }/findCourseByCourseName" method="POST" >
        <div class="content_input">
            <%--查询--%>
            <input class="my-input" type="text" name="c_name" id="c_name" placeholder="请输入课程名称"
                   aria-label="Search">
            <input class="btn btn-primary search_btn" type="submit" value="查询" >
            <%--所有--%>
            <form style="padding-left: 10%" action="${pageContext.request.contextPath }/loadCourseList" method="POST">
                <input type="submit" value="所有" class="btn btn-primary search_btn">
            </form>
        </div>
    </form>
</div>
<div style="margin-left: 20%">
    <div align="left">
        <input class="button-add" type="button" value="添加课程" onclick='location.href="${pageContext.request.contextPath }/loadCourseStudentList"'/>
    </div>
</div>
<div style="margin-left: 20%;margin-top: 80px">
    <table id="mytable">
        <thead>
        <tr>
            <th>课程名称</th>
            <th>选课人数</th>
            <th colspan="2">操作</th>
        </tr>
        </thead>

        <tbody>
        <!--  展示课程信息-->
        <%
            List<Course> courses = (List<Course>) session.getAttribute("get_courses");
            if (courses == null || courses.isEmpty()) {
        %>
        <tr>
            <td colspan="4" style="text-align: center">数据为空</td>
        </tr>
        <%
        } else {
            for (Course c : courses) {
        %>
        <tr>
            <td><%=c.getC_name()%>
            </td>
            <td><%=c.getStudentNumber()%></td>
            <td>
                <input class="button-detail" type="button" value="选课详情" onclick="location.href='acquireClassNamed'">
            </td>
            <td>
                <input class="button-add-stu" type="button" value="添加学生" onclick=add_stu()>
            </td>
        </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
</div>


<div style="margin-top: -400px; margin-left: 500px">
    <div class="part">
        <div style="font-size: 22px; background-color: #a6e1ec; width: 100px; margin-left: 80px; margin-bottom: 10px">
            添加学生
        </div>
        <%--        <form action="${pageContext.request.contextPath }/addCourseStudent" method="post">--%>
        <div>
            学号：<input type="text" name="s_id" id="s_id" size="20px" placeholder="请输入学号"><br><br>
            姓名：<input type="text" name="s_name" id="s_name" size="20px" placeholder="请输入姓名"> <br><br>
            班级：<input type="text" name="s_class" id="s_class" size="20px" placeholder="请输入班级"><br><br>
            <input type="submit" value="添加" size="30px"
                   class="button-inner"><input
                type="reset" value="取消" size="30px" onclick='location.href=("courseList.jsp")'
                style="background-color: dodgerblue; color: white; border-color: #a6e1ec; margin-left: 60px"><br><br>
            <%--        </form>--%>
        </div>
    </div>
</div>
</body>
</html>
<script src="js/jquery-1.12.3.min.js"></script>
<script>
    $(function () {
        $(".button-add-stu").click(function () {
            var name1 = $(this).parents("tr").children("td:nth-child(1)").text();//获取课程名称
            $.ajax({//传数据给controller获取课程名称
                type: "post",
                url: "${pageContext.request.contextPath}/acquireClassName",
                data: {
                    "name": name1
                },
                success: function (data) {
                    console.log(data);//在控制台输出信息。
                }
            });
        })
        $(".button-inner").click(function() {
            var s_id=document.getElementById("s_id").value;
            var s_name=document.getElementById("s_name").value;
            var s_class=document.getElementById("s_class").value;
            $.ajax({//传数据给controller获取课程名称
                type: "post",
                url: "${pageContext.request.contextPath}/addCourseStudent",
                data: {
                    "s_id": s_id,
                    "s_name": s_name,
                    "s_class": s_class
                },
                success: function (data) {
                    //console.log(data);//在控制台输出信息。
                    if(data=="true"){
                        alert("添加成功")
                        window.location.replace("${pageContext.request.contextPath }/loadCourseList");
                    }else if(data=="wronginfo"){
                        alert("输入的学生信息有误！")
                    }else if(data=="wrongs_id"){
                        alert("输入的学号有误")
                    }else if(data=="already"){
                        alert("该学生不可加入该课程")
                    }else{
                        alert("添加失败")
                    }

                }
            });
        })
        $(".button-detail").click(function () {
            var name2 = $(this).parents("tr").children("td:nth-child(1)").text();//获取课程名称
            $.ajax({//传数据给controller获取课程名称
                type: "post",
                url: "${pageContext.request.contextPath}/acquireClassNamed",
                data: {
                    "name2": name2
                },
            });
        })
    })
</script>